package com.aggregate.pay.sanstar.test.client;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.Future;

import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.mime.FileBody;
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
import org.apache.hc.client5.http.entity.mime.StringBody;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aggregate.pay.sanstar.support.HttpClient;

public class SanstarHttpClient implements HttpClient {
	
	private static Logger log = LoggerFactory.getLogger(SanstarHttpClient.class);
	
	private static final String CHARSET = "UTF-8";
	
	@Override
	public String post(String path, String reqBody) {
		log.debug("请求地址：{}", path);
		log.debug("请求内容：{}", reqBody);
		Header header = new BasicHeader("Content-Type", 
				"application/json; charset=" + CHARSET);
		SimpleHttpRequest request = SimpleRequestBuilder
		        .post()
		        .setUri(path)
		        .setHeader(header)
		        .setBody(reqBody, ContentType.APPLICATION_JSON)
		        .build();
		
		try (CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault()) {
		    httpclient.start();
		    Future<SimpleHttpResponse> future = httpclient.execute(request, null);
		    SimpleHttpResponse response = future.get();
		    log.debug("响应状态：{}", response.getCode());
		    log.debug("响应内容：{}", response.getBodyText());
		    return response.getBodyText();
		} catch (Exception e) {
            log.error("接口调用异常", e);
        }
		return null;
	}

	@Override
	public String get(String url) {
		log.debug("请求地址：{}", url);
		SimpleHttpRequest request = SimpleRequestBuilder
                .get()
                .setUri(url)
                .build();
		try (CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault()) {
            httpclient.start();
            Future<SimpleHttpResponse> future = httpclient.execute(request, null);
            SimpleHttpResponse response = future.get();
            log.debug("响应状态：{}", response.getCode());
            log.debug("响应内容：{}", response.getBodyText());
            return response.getBodyText();
        } catch (Exception e) {
            log.error("接口调用异常", e);
        }
        return null;
	}
	
    @Override
    public String post(String url, String body, File file) throws Exception {
        log.debug("请求地址：{}", url);
        log.debug("请求file：{}", file.getAbsolutePath());
        log.debug("请求body：{}", body);
        final HttpPost httpPost = new HttpPost(url);
        final FileBody bin = new FileBody(file);
        final StringBody comment = new StringBody(body, ContentType.APPLICATION_JSON);
        final HttpEntity reqEntity = MultipartEntityBuilder.create()
                .addPart("file", bin)
                .addPart("body", comment)
                .build();

        httpPost.setEntity(reqEntity);
        
        try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
            return httpclient.execute(httpPost, new HttpClientResponseHandler<String>() {

                @Override
                public String handleResponse(ClassicHttpResponse response) throws HttpException, IOException {
                    String body = EntityUtils.toString(response.getEntity());
                    log.debug("响应状态：{}", response.getCode());
                    log.debug("响应内容：{}", body);
                    return body;
                }
            });
        } catch (Exception e) {
            log.error("接口调用异常", e);
        }
        return null;
    }

    @Override
    public <T> T post(String url, String body, Class<T> clazz) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

}
